package com.weilus.controller.permission;

import com.weilus.service.cache.ImageCodeService;
import com.weilus.service.permission.UserService;
import com.weilus.util.ImageValidCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.security.Principal;

@Controller
public class LoginController{
	public static Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	ImageCodeService imageCodeService;

	@Autowired
	UserService userService;

	@GetMapping("/login.html")
	public String login(String err,Model model){
		if("1".equals(err))model.addAttribute("msg","用户名或密码错误！");
		else if("2".equals(err))model.addAttribute("msg","验证码错误！");
		return "login";
	}
	@GetMapping("/admin/loginImageCode")
	public void imageCode(String key, HttpServletResponse response){
		ImageValidCode code = new ImageValidCode();
		imageCodeService.putLoginImageCode(key,code.getCode());
		response.setContentType("image/png");
		try(
				OutputStream out = response.getOutputStream()
		) {
			code.write(out);
		}catch (IOException e){
			e.printStackTrace();
		}
	}

	/**首页*/
	@GetMapping("/admin/index")
	public String index(Model model,Principal principal){
		model.addAttribute("userName",principal.getName());
		return "common/index";
	}


	@GetMapping("/common/{html}.html")
	public String commonHtml(@PathVariable("html")String html){
		return "common/"+html;
	}

}
